﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XSTDZ.ERP.Application.DataContracts;
using XSTDZ.ERP.Application;
using XSTDZ.ERP.Infrastructure;
using XSTDZ.ERP.WebApp.Models;
using XSTDZ.ERP.Infrastructure.Authentication;

namespace XSTDZ.ERP.WebApp.Controllers
{
    public class RoleController : Controller
    {
        private readonly IPermisstionService permissionServiceImpl = ServiceLocator.Instance.GetService<IPermisstionService>();	
        //
        // GET: /Role/
        //角色列表
        public ActionResult Index()
        {
            return View();
        }
        #region 添加角色
        [HttpGet]
        [NoAuthorizated("Add")]
        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        [NoAuthorizated("Add")]
        public ActionResult Add(RoleDto roleDto)
        {
            if (ModelState.IsValid)
            {
                permissionServiceImpl.AddRole(roleDto);
                ViewBag.PostState =9;
            }
            return View();
        }
        #endregion
        #region 更新角色
        [HttpGet]
        [NoAuthorizated("Edit")]
        public ActionResult Edit(Guid roleId)
        {
            var role = permissionServiceImpl.GetRole(roleId);
            return View(role);
        }
        [HttpPost]
        [NoAuthorizated("Edit")]
        public ActionResult Edit(RoleDto roleDto)
        {
            if (ModelState.IsValid)
            {
                permissionServiceImpl.UpdateRole(roleDto);
                ViewBag.PostState = 9;
            }
            return View();
        }
        #endregion
        #region 删除角色
        [HttpGet]
        [NoAuthorizated("Delete")]
        public ActionResult Delete(Guid roleId)
        {
            permissionServiceImpl.DeleteRole(roleId);
            return Content("ok");
        }
        #endregion
        #region 辅助显示
        /// <summary>
        /// 前台由ligirueGird显示
        /// </summary>
        /// <returns></returns>
         [NoAuthorizated("GetJsonOfRoles")]
        public ActionResult GetJsonOfRoles()
        {
            var roles = permissionServiceImpl.GetAllRole();
            if (roles != null && roles.Count() != 0)
            {
                var jsonResult = roles.Select(r => new
                {
                    Key = r.Id,
                    Code = r.Code,
                    Name = r.Description
                });

                var target = Newtonsoft.Json.JsonConvert.SerializeObject(jsonResult);
                target = "{\"Rows\":" + target + ",\"Total\":\"10\"}";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                return Content(target);
            }
            return Content("");
        }
        #endregion
        #region 角色权限
        [HttpPost]
        [NoAuthorizated("SetRolePermissions")]
        public ActionResult SetRolePermissions()
        {
            string roleId=Request.Form["roleid"];
            string orgPermission = Request.Form["orgpermissions"];
            string uiPermission = Request.Form["uipermissions"];
            string typeName = Request.Form["typeName"];//类别名称

            RoleModel roleModel=new RoleModel();
            var uiIds = roleModel.ConvertStringToGuidList(uiPermission);
            var ids = roleModel.ConvertStringToGuidList(orgPermission);

            //permissionServiceImpl.AddOrUpdatePermissionsOfRole(Guid.Parse(roleId), uiIds.AsQueryable(), ids.AsQueryable());
            var permissionIds = ids.Concat(uiIds);
            permissionServiceImpl.AddOrUpdatePermissionsOfRole(Guid.Parse(roleId), null, permissionIds.AsQueryable(),typeName);
            return Content("ok");
        }
        #endregion
    }
}
